﻿using JustCleanIt.Common;
using JustCleanIt.DataStructure;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;

namespace JustCleanIt.Database
{
    public class CustomerRepository : ICustomerRepository
    {
        private static LogManager myLogger = LogManager.GetLogger(typeof(CustomerRepository));
        private List<Customer> myCustomers = new List<Customer>();
        private IDatabaseManager myDatabaseManager;

        public CustomerRepository()
        {
            myLogger.LogInfo("CustomerRepository: Enter");

            myDatabaseManager = new XmlDatabaseManager();
            myCustomers = myDatabaseManager.Load();

            myLogger.LogInfo("CustomerRepository: Exit");
        }

        //CRUD Operations

        //1. CREAT
        public string AddNewCustomer(Customer newCustomer, string password2)
        {
            myLogger.LogInfo("AddNewCustomer: Enter");
            if (newCustomer == null)
            {
                throw new ArgumentNullException("newCustomer");
            }

            string result = Utility.ValidateNewCustomerDetails(myCustomers, newCustomer, password2);

            if (string.IsNullOrWhiteSpace(result))
            {
                newCustomer.ID = string.Format("INBLR{0:000000}", myCustomers.Count + 1); ;
                myCustomers.Add(newCustomer);

                result = "Customer created! ID - " + newCustomer.ID;

                myDatabaseManager.Save(myCustomers);
            }

            myLogger.LogInfo("AddNewCustomer: Exit");
            return result;

        }

        //2. RETRIEVE /ALL
        public List<Customer> GetAllCustomers()
        {
            myLogger.LogInfo("GetAllCustomers: Enter");

            myLogger.LogInfo("GetAllCustomers: Exit");
            return myCustomers;
        }

        //2. RETRIEVE /By CustomerId
        public Customer GetCustomerByName(string customerName)
        {
            myLogger.LogInfo("GetCustomerByName: Enter");

            myLogger.LogInfo("GetCustomerByName: Exit");
            return myCustomers.Find(b => b.FirstName == customerName || b.LastName == customerName);
        }

        public bool IsCustomerRegistered(string email)
        {
            myLogger.LogInfo("IsCustomerAlreadyRegistered: Enter");

            bool result = false;

            result = myCustomers.Exists(e => e.Email == email);

            myLogger.LogInfo("IsCustomerAlreadyRegistered: Exit");
            return result;
        }

        //3. UPDATE
        public bool UpdateACustomer(Customer updatedCustomer)
        {
            myLogger.LogInfo("UpdateACustomer: Enter");
            if (updatedCustomer == null)
            {
                throw new ArgumentNullException("updatedCustomer");
            }
            int idx = myCustomers.FindIndex(b => b.FirstName == updatedCustomer.FirstName || b.LastName == updatedCustomer.LastName);
            if (idx == -1)
            {
                return false;
            }
            myCustomers.RemoveAt(idx);
            myCustomers.Add(updatedCustomer);

            myDatabaseManager.Save(myCustomers);

            myLogger.LogInfo("UpdateACustomer: Exit");

            return true;
        }

        //4. DELETE
        public bool DeleteACustomer(string name)
        {
            myLogger.LogInfo("DeleteACustomer: Enter");
            int idx = myCustomers.FindIndex(b => b.FirstName == name || b.LastName == name);
            if (idx == -1)
            {
                return false;
            }
            myCustomers.RemoveAll(b => b.FirstName == name || b.LastName == name);

            myDatabaseManager.Save(myCustomers);

            myLogger.LogInfo("DeleteACustomer: Exit");

            return true;
        }
    }
}